package org.tio.sitexxx.web.server.controller.minip;

import com.alibaba.fastjson.JSON;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.Consultant;
import org.tio.sitexxx.service.model.main.Interest;
import org.tio.sitexxx.service.model.main.Member;
import org.tio.sitexxx.service.service.qj.SkipService;
import org.tio.utils.resp.Resp;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestPath(value="/skip")
public class SkipController {
    /**
     * 用户在咨询客服前先判断一下之前是否支付过皆在服务期内
     * @param openid
     * @param consultant_id
     * @return
     */
    @RequestPath(value = "/skip/isValid")
    public List<Map<String, Object>>  isValid(String openid,int consultant_id){
        Map<String ,Object>map=new HashMap<>(6);
        Map<String,Integer> cid = SkipService.me.findMId(openid);
        Integer member_id=cid.get("id");
        System.out.println("cid:"+JSON.toJSONString(cid)+"openid:"+openid);
        List<Map<String, Object>> validTime = SkipService.me.findIsValid(member_id);
        System.out.println("validtime:"+JSON.toJSONString(validTime));
        if(validTime==null){
            map.put("consultant_id",consultant_id);
            map.put("member_id",member_id);
            map.put("valid",false);
            validTime.add(map);
            return validTime;
        }
        return validTime;
    }

    /**
     * 废弃
     * @param cid
     * @param mid
     * @return
     */
    @RequestPath(value = "/qj/kf/over")
    public boolean timeover(int cid,int mid){
        try {
            boolean update = SkipService.me.timeend(cid, mid);
            return update;
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }

    /**
     * 新旧数据库过渡
     * @param request
     * @param response
     * @return
     */
    @RequestPath(value="/change")
    public boolean test(HttpRequest request, HttpResponse response) {
        return SkipService.me.member_change_to_user();
    }

    @RequestPath(value="/changemember")
    public Resp member(){
       /* Consultant consultant=Consultant.dao.findById(2601);//获取顾问，增加余额
        int consultation_fees=consultant.getConsultationFees();
        int balance=consultant.getBalance();
        int total_balance=balance+consultation_fees*100/2;//以分为单位，直接整除
        consultant.setBalance(total_balance);
        consultant.update();
//        SkipService.me.member_nickname();*/
//       String sql="select * from member";
//       List<Member>list=Member.dao.find(sql);
//       boolean result= Db.tx(new IAtom() {
//           @Override
//           public boolean run() throws SQLException {
//               list.forEach(member -> {
//                   if(member.getTestAnswer()!=null){
//                       String answer=member.getTestAnswer();
//                       String newanswer=answer.replace("p70m18mpm.bkt.clouddn.com","qjqn.foxcodes.cn");
//                       member.setTestAnswer(newanswer);
//                       member.update();
//                   }
//               });
//               return true;
//           }
//       });
//        System.out.println("result:"+result);
//        return Resp.ok();
        Interest interest=Interest.dao.findById(29);
        float rate=interest.getCommissionRate();
        System.out.println("rate:"+rate);
        System.out.println(8*100*rate);
        Float r=8*100*rate;
        int balance=r.intValue();
        System.out.println("balance:"+balance);
        return Resp.ok("ok");
    }

}
